package icu.sehnen.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import icu.sehnen.user.entity.Permission;
import icu.sehnen.user.entity.Role;
import icu.sehnen.user.entity.UserRoleRelation;
import icu.sehnen.user.mapper.RoleMapper;
import icu.sehnen.user.mapper.RolePermissionRelationMapper;
import icu.sehnen.user.mapper.UserRoleRelationMapper;
import icu.sehnen.user.service.IRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
/**
 * <p>
 * 后台用户角色表 服务实现类
 * </p>
 *
 * @author icu.sehnen
 * @since 2021-04-27
 */
@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements IRoleService {
    @Autowired
    RoleMapper roleMapper;
    @Autowired

    RolePermissionRelationMapper rolePermissionRelationMapper;
    @Autowired
    UserRoleRelationMapper userRoleRelationMapper;
    @Override
    public List<Role> getRoleList() {
//        查询所有角色
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq(false,"id","");
        List<Role> roleList=roleMapper.selectList(queryWrapper);
        roleList.forEach(role -> {
            List<Permission> permissionList =rolePermissionRelationMapper.getPermissionListByRoleId(role.getId());
               List<Permission> permissionList1= buildMenuTree(permissionList,0l);
                role.setChildren(permissionList1);
        });
        return  roleList;
    }

    @Override
    public Role getRoleById(Long id) {
        Role role = roleMapper.selectById(id);
        List<Permission> permissionList =rolePermissionRelationMapper.getPermissionListByRoleId(role.getId());
        List<Permission> permissionList1= buildMenuTree(permissionList,0l);
        role.setChildren(permissionList1);
        return  role;
    }

    @Override
    public List<Long> getRoleListByUserId(Long id) {
        return  roleMapper.getRoleListByUserId(id);
    }

    @Override
    public void addRoleforUserId(List<Long> ids,Long id) {
        UpdateWrapper<UserRoleRelation> objectUpdateWrapper = new UpdateWrapper<>();
        objectUpdateWrapper.eq("admin_id",id);
        userRoleRelationMapper.delete(objectUpdateWrapper);
        for (Long aLong : ids) {
            UserRoleRelation build = UserRoleRelation.builder().adminId(id).roleId(aLong).build();
            userRoleRelationMapper.insert(build);
        }
    }

    @Override
    public List<Long> getIRoleIdsByUserId(Long id) {
        return  userRoleRelationMapper.getIRoleIdsByUserId(id);
    }


    private List<Permission> buildMenuTree(List<Permission> menuList, Long pid) {
        List<Permission> treeList = new ArrayList<>();
        menuList.forEach(menu -> {
            if (pid.equals(menu.getPid())) {
                menu.setChildren(buildMenuTree(menuList, menu.getId()));
                treeList.add(menu);
            }
        });
        return treeList;
    }
}
